package com.sy.simpleegg.room.repository

import com.sy.simpleegg.platform.BackstagePlatform
import com.sy.simpleegg.room.dao.GoodDao
import com.sy.simpleegg.room.entity.GoodEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class GoodRepository(private val goodDao: GoodDao) {

    suspend fun getAllGoods(): MutableList<GoodEntity>? {
        return withContext(Dispatchers.IO) {
            goodDao.getAll()
        }
    }

    suspend fun getLastGoods(platform: BackstagePlatform): MutableList<GoodEntity>? {
        return withContext(Dispatchers.IO) {
            goodDao.getLastGoods(platform)
        }
    }

    suspend fun insert(goodEntity: GoodEntity) {
        withContext(Dispatchers.IO) {
            goodDao.insert(goodEntity)
        }
    }

    suspend fun insertAll(goodEntity: MutableList<GoodEntity>) {
        withContext(Dispatchers.IO) {
            goodDao.insertAll(goodEntity)
        }
    }

    suspend fun delete(goodEntity: GoodEntity) {
        withContext(Dispatchers.IO) {
            goodDao.delete(goodEntity)
        }
    }

    suspend fun deleteAll() {
        withContext(Dispatchers.IO) {
            goodDao.deleteAll()
        }
    }
}